/**
 *
 * @description: 电影分类
 * @author: junyong.hong
 * @createTime: 2019/2/23
 * @version: 1.0.0.0
 * @history:
 *    1、
 *    2、
 *
 */
const mongoose = require('mongoose')
const Schema = mongoose.Schema
const ObjectId = Schema.Types.ObjectId

const CategorySchema = new Schema({
    // 分类名称
    name: {
        // 唯一
        unique: true,
        type: String
    },
    // 分类里的电影id
    movies: [{
        type: ObjectId,
        ref: 'Movie'
    }],
    meta: {
        createdAt: {
            type: Date,
            default: Date.now()
        },
        updatedAt: {
            type: Date,
            default: Date.now()
        }
    }
})

CategorySchema.pre('save', function (next) {
    // 如果是新增的
    if (this.isNew) {
        this.meta.createdAt = this.meta.updatedAt = Date.now()
    } else {
        this.meta.updatedAt = Date.now()
    }

    next()
})

const Category = mongoose.model('Category', CategorySchema)
